﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace NasuTek.Shion
{
    public class Configuration
    {
        bool _NoHTMLRenderingOutsideClient = true;

        [System.ComponentModel.Category("HTML Renderer")]
        [System.ComponentModel.Description("Enables or Disables HTML Tag Rendering.")]
        public bool NoHTMLRenderingOutsideClient
        {
            get { return _NoHTMLRenderingOutsideClient; }
            set { _NoHTMLRenderingOutsideClient = value; }
        }

        List<Server> _Servers = new List<Server>();

        [System.ComponentModel.Category("Server Data")]
        [System.ComponentModel.Description("Allows you to add or delete servers as well as modify existing ones. Note these settings do not apply till restart of Shion except for Ignore Modifications")]
        public List<Server> Servers
        {
            get { return _Servers; }
            set { _Servers = value; }
        }

        string _Theme = "Default Theme";

        [System.ComponentModel.Category("HTML Themer")]
        [System.ComponentModel.Description("The name of the default set theme")]
        [System.ComponentModel.ReadOnly(true)]
        public string Theme
        {
            get { return _Theme; }
            set { _Theme = value; }
        }

        string _Name;

        [System.ComponentModel.Category("Registration")]
        [System.ComponentModel.Description("Name of Shion Registrant")]
        [System.ComponentModel.ReadOnly(true)]
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        string _Hash;

        [System.ComponentModel.Category("Registration")]
        [System.ComponentModel.Description("Hash of Shion Registrant")]
        [System.ComponentModel.ReadOnly(true)]
        public string Hash
        {
            get { return _Hash; }
            set { _Hash = value; }
        }

        public Server GetServer(string name)
        {
            Server server = null;
            foreach (Server i in Servers)
            {
                if (i.Address == name)
                {
                    server = i;
                    break;
                }
            }
            return server;
        }

        public void RemoveServer(string name)
        {
            Servers.Remove(GetServer(name));
        }
    }

    public class Ignore
    {
        string _HostmaskRegEx;
        bool _IgnoreJoinAndPart, _IgnorePrivateMessages, _IgnorePublicMessages, _IgnoreNotices;

        public bool IgnoreJoinAndPart
        {
            get { return _IgnoreJoinAndPart; }
            set { _IgnoreJoinAndPart = value; }
        }

        public bool IgnorePrivateMessages
        {
            get { return _IgnorePrivateMessages; }
            set { _IgnorePrivateMessages = value; }
        }

        public bool IgnorePublicMessages
        {
            get { return _IgnorePublicMessages; }
            set { _IgnorePublicMessages = value; }
        }

        public bool IgnoreNotices
        {
            get { return _IgnoreNotices; }
            set { _IgnoreNotices = value; }
        }

        public string HostmaskRegEx
        {
            get { return _HostmaskRegEx; }
            set { _HostmaskRegEx = value; }
        }
    }

    public class Server
    {
        public Server() { }
        public Server(string address, string nick, string name, string userName, string nickServPassword, string operUserName, string operPass)
        {
            Address = address;
            Nick = nick;
            Name = name;
            UserName = userName;
            NickServPassword = nickServPassword;
            OperUserName = operUserName;
            OperPassword = OperPassword;
        }

        string _Address, _Nick, _Name, _UserName, _NickServPassword, _OperUserName, _OperPassword;

        public string OperUserName
        {
            get { return _OperUserName; }
            set { _OperUserName = value; }
        }

        public string OperPassword
        {
            get { return _OperPassword; }
            set { _OperPassword = value; }
        }

        List<Ignore> _Ignores = new List<Ignore>();

        public List<Ignore> Ignores
        {
            get { return _Ignores; }
            set { _Ignores = value; }
        }

        public string NickServPassword
        {
            get { return _NickServPassword; }
            set { _NickServPassword = value; }
        }

        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public string Nick
        {
            get { return _Nick; }
            set { _Nick = value; }
        }

        public string Address
        {
            get { return _Address; }
            set { _Address = value; }
        }
        string[] _Channels = new string[] { };

        public string[] Channels
        {
            get { return _Channels; }
            set { _Channels = value; }
        }

        public string GetChannel(string name)
        {
            string server = null;
            foreach (string i in Channels)
            {
                if (i == name)
                {
                    server = i;
                    break;
                }
            }
            return server;
        }

        public Ignore GetIgnore(string name)
        {
            Ignore server = null;
            foreach (Ignore i in Ignores)
            {
                if (Regex.IsMatch(name,i.HostmaskRegEx))
                {
                    server = i;
                    break;
                }
            }
            return server;
        }

        public void RemoveChannel(string name)
        {
            List<string> channels = new List<string>(Channels);
            channels.Remove(name);
            Channels = channels.ToArray();
        }

        public void AddChannel(string name)
        {
            List<string> channels = new List<string>(Channels);
            channels.Add(name);
            Channels = channels.ToArray();
        }

    }
}